from django.shortcuts import render,HttpResponseRedirect

# Create your views here.
import  requests
from .models import Message

def message_board(request):
    return render(request, template_name='nav.html')

def upmessage_board(request):
    return render(request, template_name='update.html')

def addmessage_board(request):
    return render(request, template_name='add.html')

def checkmessage_board(request):
    m_all = Message.objects.all()
    return render(request, template_name='nav.html', context={'mes': m_all})

def add_message(request):
    if request.method == 'GET':
        name = request.GET.get("name", None)
        email = request.GET.get("email", None)
        address = request.GET.get("address", None)
        message = request.GET.get("message", None)

        m = Message(m_name=name, m_email=email, m_address=address, m_message=message)
        m.save()
        m_all = Message.objects.all()
        for new_m_all in m_all:
            print(new_m_all.m_name)
        return HttpResponseRedirect('/message/message3')



def update_message(request):
    if request.method == 'GET':
        id = request.GET.get("id", None)
        um = Message.objects.get(id=id)
        name = request.GET.get("name", None)
        email = request.GET.get("email", None)
        address = request.GET.get("address", None)
        message = request.GET.get("message", None)

        print(id, name)

        um.m_name = name
        um.m_email = email
        um.m_address = address
        um.m_message = message

        um.save()
        return render(request, template_name='nav.html')


